<template>
<!-- 积分汇总 -->
  <div class="wise-integral-total">
    <div class="search">
      <van-search v-show="isSeo" left-icon="search" v-model="loginId" :clearable="false" placeholder="请输入工号/姓名" @search="handleSearch" />
      <date-time ref="dataTimeRefmonth" datetype="year-month" @date="handleChange" />
    </div>
    <van-list
      v-model="loading"
      loading-text="加载中"
      :finished="finished"
      finished-text="没有更多了"
      @load="onload"
    >
      <div class="detail-info" v-for="(item,index) in list" :key="index" @click="handleOpenList(item.LOGIN_ID, item.LOGIN_NAME)">
        <img class="item-img-user" src="~@/assets/imgs/publicline/board/icon-users.png" />
        <div class="info-content">
          <div class="info-detail">
            <span class="info-name">{{ item.LOGIN_NAME }}</span>
            <span class="info-flag">{{ item.LOGIN_ID }}</span>
          </div>
          <div class="info-tc">
            <div class="info-desc">岗位：{{ item.CALC_TYPE_NAME }}</div>
            <div>
              <span class="info-desc">积分：</span>
              <span class="item-value" >{{ item.TOTAL_SCO }}</span>
            </div>
          </div>
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>
import dateTime from '@/components/common/dateTime.vue'
import { Toast } from 'vant'
import * as dayjs from "dayjs"
import { accountDate } from '@/api/common.js'

import { monthIntegral } from '@/api/publicline/wise.js'

export default {
  components: { dateTime },
  data() {
    return {
      isSeo: this.$isCeo, // 是否是ceo
      loginId: '',
      monthId: '',
      queryParams: {
        pageNum: 1,
        pageSize: 15
      },
      loading: false,
      finished: false,
      list: [],
    }
  },
  mounted() {
    this.getAccountMonthDate()
  },
  beforeDestroy() {
    Toast.clear()
  },
  methods: {
    // 获取月账期
    async getAccountMonthDate() {
      const res = await accountDate({ PARAMCODE: 'M' })
      const { result, success } = res
      if (success) {
        this.monthId = dayjs(result[0].value).format('YYYY-MM')
        this.$refs.dataTimeRefmonth.initDateTime(this.monthId)
        this.loadData(true)
      }
    },
    handleSearch(val) {
      this.loginId = val
      this.loadData(true)
    },
    // 获取改变的账期
    handleChange(val) {
      this.monthId = val
      this.loadData(true)
    },
    async loadData(force) {
      // 改变搜索条件，重新查询
      if ( force ) {
        this.queryParams.pageNum = 1
        this.list = []
        this.finished = false
      }
      this.loading = true
      const params = {
        ...this.queryParams,
        orgId: this.$gridId,
        loginId: this.isSeo ? this.loginId : this.$dataPermission,
        monthId: dayjs(this.monthId).format('YYYYMM')
      }
      const res = await monthIntegral(params)
      const { result, success } = res
      if(success) {
        this.loading = false
        if(result.list.length < this.queryParams.pageSize) {
          this.finished = true
          Toast({
            message: '已全部加载',
            duration: 500,
            position: 'bottom',
            forbidClick: 'true'
          });
        }
        this.list = this.list.concat(result.list)
        Toast.clear()
      }
    },
    onload() {
      if ( !this.monthId ) return
      this.loading = false
      if(this.finished === false) {
        Toast({
          type:'loading',
          duration: 0,
          message: '正在加载',
          position: 'bottom',
          forbidClick: 'true'
        })
        this.queryParams.pageNum = parseInt(this.queryParams.pageNum) + 1
        this.loadData()
      } else {
        Toast({
          message: '已全部加载',
          duration: 500,
          position: 'bottom',
          forbidClick: 'true'
        })
      }
    },
    handleOpenList(loginId, loginName) {
      this.$open.call(this, '/publicline/wise/integralList',
        {
          loginId,
          loginName,
          type: 'month',
          time: dayjs(this.monthId).format('YYYYMM'),
        }
      )
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@/assets/less/person.less';

.wise-integral-total {
  .item-value {
    text-decoration: underline;
  }
}
</style>